package com.yanchangapp.ui.presenter;

import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;
import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.response.MsgContentResponse;
import com.yanchangapp.ui.view.MsgCntAtView;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 消息内容的Presenter
 */
public class MsgCntAtPresenter extends BasePresenter<MsgCntAtView> {

    public MsgCntAtPresenter(BaseActivity context) {
        super(context);
    }

    public void refreshMsgList(Integer msgType){
        HeadApiRetrofit.getInstance().queryMessageList(msgType, AppConstant.FIRST_PAGE_NUM)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MsgContentResponse>() {
                    @Override
                    public void onNext(MsgContentResponse msgContentResponse) {
                        if (msgContentResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (getView() != null){
                                getView().refreshMsgSuccess(msgContentResponse.getData().getList());
                            }
                        }else {
                            if (getView() != null){
                                getView().refreshMsgFailure(msgContentResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().refreshMsgFailure("刷新消息列表失败");
                        }
                    }
                });
    }

    public void loadMoreMsgList(Integer msgType,int currentPage){
        HeadApiRetrofit.getInstance().queryMessageList(msgType, currentPage)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MsgContentResponse>() {
                    @Override
                    public void onNext(MsgContentResponse msgContentResponse) {
                        if (msgContentResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            getView().loadMoreMsgSuccess(msgContentResponse.getData().getList());
                        }else {
                            getView().loadMoreMsgFailure(msgContentResponse.getMsg());
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        getView().loadMoreMsgFailure("加载消息列表失败");
                    }
                });
    }


    public void setMsgReadedState(List<Integer> setList){
        HeadApiRetrofit.getInstance().setReadedMessageList(setList)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (baseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (getView() != null){
                                getView().setReadedSuccess();
                            }
                        }else {
                            if (getView() != null){
                                getView().setReadedFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        getView().setReadedFailure("设置已读失败");
                    }
                });
    }
}
